package com.gy.boot.controller;

import com.gy.boot.common.aspect.Log;
import com.gy.boot.common.dao.IDao;
import com.gy.boot.common.dao.IEntity;
import com.gy.boot.common.dao.SqlID;
import com.gy.boot.common.util.HttpContextUtil;
import com.gy.boot.common.util.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

//@Permissions
@RestController
@RequestMapping("user")
public class UserController {

    @GetMapping("index")
    public R index() {
        Long userId = HttpContextUtil.getJwtUserId();
        return R.ok(userId);
    }

    @GetMapping("listUser")
    public R listUser() {
        IEntity params = IEntity.create(2).set("user_id", 1)
                .set("listId", new int[]{1, 1, 2, 2, 3, 4});
        List<IEntity> listUser = IDao.listS(SqlID.User_listUser, params);
        return R.ok().set("listUser", listUser);
    }


    @Log("保存用户")
    @GetMapping("save")
    public R saveUser(@RequestParam Map<String, Object> params) {
        IEntity newUser = IEntity.create(params);
        newUser.put("name", "test123");
        newUser.put("phone", "112233");
        newUser.put("sex", "男");
        IDao.insertS(SqlID.User_insertUser, newUser);
        return R.ok().setAll(newUser);
    }

    @Log("修改用户")
    @GetMapping("update")
    public R updateUser(@RequestParam Map<String, Object> params) {
        long userId = 100L;
        IEntity updateUser = IEntity.create(params);
        updateUser.put("user_id", userId);
        updateUser.put("name", "test123");
        updateUser.put("phone", "112233");
        updateUser.put("sex", "女");
        IDao.updateS(SqlID.User_updateUser, updateUser);
        IEntity user = IDao.getS(SqlID.User_getUser, userId);
        return R.ok().set("user", user);
    }

    @Log("删除用户")
    @GetMapping("delete")
    public R deleteUser(Long userId) {
        userId = userId == null ? 100 : userId;
        IDao.deleteS(SqlID.User_deleteUser, userId);
        IEntity user = IDao.getS(SqlID.User_getUser, userId);
        return R.ok().set("user", user);
    }

}
